﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
 * 摄像机
 */
public class FollowCamera : MonoBehaviour
{
    private const float ZOOM_IN_DURATION = 0.2f;        // [sec] 推近所持续的时间
    private const float ZOOM_OUT_DURATION = 1.5f;       // [sec] 拉远持续的时间

    public Vector3 m_position_offset;
    public Vector3 m_position_offset_zoom_in_h;
    public Vector3 m_position_offset_zoom_in_v;

    private GridMove m_player_grid;

    public Transform m_target;
    private bool m_zoom_in;
    private float m_zoom_in_timer = 0.0f;
    private bool IsH;
    // Start is called before the first frame update
    void Start()
    {
        IsH = false;
        m_zoom_in = false;
        m_zoom_in_timer = ZOOM_IN_DURATION;
        m_position_offset = new Vector3(0, 4, -3);
        m_position_offset_zoom_in_v = new Vector3(4, 3, -1);
        m_position_offset_zoom_in_h = new Vector3(0, 3, -4);
    }

    void Update()
    {
        m_target = GameObject.FindWithTag("Player").transform;
        m_player_grid = m_target.GetComponent<GridMove>();
        
    }

    private void LateUpdate()
    {
        float rate = 0.0f;
        transform.LookAt(m_target.position + new Vector3(0, 1, 0));
        if (m_zoom_in)
        {
            rate = m_zoom_in_timer / ZOOM_IN_DURATION;
            rate = Mathf.Clamp01(rate);
            rate = Mathf.Sin(rate * Mathf.PI / 2.0f);
            rate = Mathf.Pow(rate, 0.5f);

        }
        else
        {
            rate = m_zoom_in_timer / ZOOM_OUT_DURATION;
            rate = Mathf.Clamp01(rate);
            rate = Mathf.Sin(rate * Mathf.PI / 2.0f);
            rate = Mathf.Pow(rate, 0.5f);

            // 计时器值为 0.0 ~ 1.0 时，缩放率值处于 0.0 ~ 1.0
            rate = 1.0f - rate;
        }

        // 计算位置偏移

        if (IsH)
        {
            Vector3 offset = Vector3.Lerp(m_position_offset, m_position_offset_zoom_in_h, rate);
            transform.position = m_target.position + offset;
        }
        else
        {
            Vector3 offset = Vector3.Lerp(m_position_offset, m_position_offset_zoom_in_v, rate);
            transform.position = m_target.position + offset;
        }


        // 计算视角，设定
        float fov = Mathf.Lerp(60.0f, 20.0f, rate);
        this.GetComponent<Camera>().fieldOfView = fov;

        m_zoom_in_timer += Time.deltaTime;
    }
    public void OnAttack()
    {
        if (Mathf.Abs(GameObject.FindWithTag("Player").GetComponent<GridMove>().GetDirection().x) >= 0.5f)
            IsH = true;
        else IsH = false;
        if (!m_zoom_in)
        {
            m_zoom_in = true;

            if (m_zoom_in_timer < ZOOM_IN_DURATION)
            {

                float rate;

                // 根据计时器求出缩放率
                rate = m_zoom_in_timer / ZOOM_OUT_DURATION;
                rate = Mathf.Clamp01(rate);
                rate = Mathf.Sin(rate * Mathf.PI / 2.0f);
                rate = Mathf.Pow(rate, 0.5f);
                rate = 1.0f - rate;

                // 根据缩放率，对推近时的计时器值进行逆变换
                rate = Mathf.Pow(rate, 1.0f / 0.3f);
                rate = Mathf.Asin(rate) / (Mathf.PI / 2.0f);
                m_zoom_in_timer = ZOOM_IN_DURATION * rate;


            }
            else
            {
                m_zoom_in_timer = 0.0f;
            }
        }
    }
    public void EndAttack()
    {
        if (m_zoom_in)
        {
            m_zoom_in = false;

            if (m_zoom_in_timer < ZOOM_OUT_DURATION)
            {

                float rate;

                // 根据计时器求出缩放率
                rate = m_zoom_in_timer / ZOOM_IN_DURATION;
                rate = Mathf.Clamp01(rate);
                rate = Mathf.Sin(rate * Mathf.PI / 2.0f);
                rate = Mathf.Pow(rate, 0.5f);
                rate = 1.0f - rate;

                // 根据缩放率，对推近时的计时器值进行逆变换
                rate = Mathf.Pow(rate, 1.0f / 0.3f);
                rate = Mathf.Asin(rate) / (Mathf.PI / 2.0f);
                m_zoom_in_timer = ZOOM_OUT_DURATION * rate;

            }
            else
            {
                m_zoom_in_timer = 0.0f;
            }
        }
    }
}
